<html>
<head>
	<title>Demo3-2-1</title>
</head>
	
<body>
	<table border="1">
<?php 
//加载XML文档
$xml = new SimpleXMLElement('product.xml', null, true);

//添加一条产品信息
$title = 'SimpleXMLElement从入门到精通';
$price = 39.9;

$newNode = $xml->addChild('product');
$newNode->addAttribute('id', 6);
$newNode->addChild('title', $title);
$newNode->addChild('price', $price);

//获取每一条商品信息，并显示在表格中
foreach ($xml->product as $row) {
	echo <<<PRODUCT
		<tr>
			<td>{$row->title}</td>
			<td>{$row->price}</td>
		</tr>
PRODUCT;
}

//修改ID为1的产品的ID属性为11
$toChangeNode = $xml->xpath('//product[@id="1"]');
$toChangeNode[0]['id'] = "11";

//保存XML文档
$xml->saveXML('productNew.xml');
?>

	</table>	
</body>
</html>
